Assignment Operators in C

An assignment operator is used for assigning a value to a variable. The most common assignment operator is “=”.

Operator Table

S. No. Symbol Operator Description Syntax
1 = Simple Assignment Assign the value of the right operand to the left operand. a = b
2 += Plus and assign Add the right operand and left operand and assign this value to the left operand. a += b
3 -= Minus and assign Subtract the right operand and left operand and assign this value to the left operand. a -= b
4 *= Multiply and assign Multiply the right operand and left operand and assign this value to the left operand. a *= b
5 /= Divide and assign Divide the left operand with the right operand and assign the result to the left operand. a /= b
6 %= Modulus and assign Assign the remainder in the division of the left operand with the right operand to the left operand. a %= b

Example

int main() {
    int a = 25, b = 5;

    printf("a = b: %d\n", a = b);
    printf("a += b: %d\n", a += b);
    printf("a -= b: %d\n", a -= b);
    printf("a *= b: %d\n", a *= b);
    printf("a /= b: %d\n", a /= b);
    printf("a %%= b: %d\n", a %= b);
    return 0;
        

Output

a = b : 5
a + b =  10
a – b = 5
a * b = 25
a / b = 5
a % b = 0